﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;
public partial class WebSite_t_paper_check : System.Web.UI.Page
{
    //public static DataSet set = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null || Session["UserIdentity"].ToString() != "1")
        {
            Response.Redirect("Default.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString["id"] == null)
                {
                    Response.Redirect("t_data_contrast.aspx", false);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    return;
                }
                int QuestionId = Convert.ToInt32(Request.QueryString["id"]);
                DataSet set = new DataSet();
                DataSet ds = new DataSet();
                QuestionnaireHandler QHandler = new QuestionnaireHandler();
                ds = QHandler.GetQuestionById(QuestionId);
                string topic = " ", path = " ";
                topic = ds.Tables[0].Rows[0][1].ToString();
                path = ds.Tables[0].Rows[0][4].ToString();
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                XmlNodeReader reader = new XmlNodeReader(doc);
                set.ReadXml(reader);
                reader.Close();
                this.Repeater1.DataSource = set;
                this.Repeater1.DataBind();
                this.Label1.Text = "您所编辑的问卷为:";
            }
            catch(Exception ex)
            {
                //跳转到错误处理页面。
                Session["ErrorMessage"] = ex.Message;
                Session["AtPage"] = "t_paper_check.aspx";
                Response.Redirect("t_error.aspx", false);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                return;
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string UserId = Session["UserId"].ToString();
            this.Button1.Visible = false;
            DataSet set = new DataSet();
            QuestionnaireHandler QHandler = new QuestionnaireHandler();
            XmlDocument doc = new XmlDocument();
            DataSet ds = new DataSet();
            ds = QHandler.GetQuestionByUserId(UserId);
            string path = " ";
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                path = ds.Tables[0].Rows[j][4].ToString();
                doc.Load(path);
                XmlNodeReader reader = new XmlNodeReader(doc);
                set.ReadXml(reader);
                reader.Close();
            }
            this.Repeater1.DataSource = set;
            this.Repeater1.DataBind();
            this.Button1.Visible = false;

            this.Label1.Text = "您编辑的所有问卷为:";
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message;
            Session["AtPage"] = "t_paper_check.aspx";
            Response.Redirect("t_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            string UserId = Session["UserId"].ToString();
            if (e.CommandName == "delete")
            {
                DataSet set = new DataSet();
                QuestionnaireHandler QHandler = new QuestionnaireHandler();
                XmlDocument doc = new XmlDocument();
                DataSet ds = new DataSet();
                ds = QHandler.GetQuestionByUserId(UserId);
                string path = " ";
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    path = ds.Tables[0].Rows[j][4].ToString();
                    doc.Load(path);
                    XmlNodeReader reader = new XmlNodeReader(doc);
                    set.ReadXml(reader);
                    reader.Close();
                }
                for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                {
                    if (Convert.ToInt32(e.CommandArgument) == Convert.ToInt32(set.Tables[0].Rows[i][1]))
                    {

                        DataSet Ds = new DataSet();
                        Ds = QHandler.GetQuestionById(Convert.ToInt32(set.Tables[0].Rows[i][1]));
                        File.Delete(Ds.Tables[0].Rows[0][4].ToString());
                        QHandler.DeleteQuestionnaireResultsById(Convert.ToInt32(set.Tables[0].Rows[i][1]));
                        QHandler.DeleteQuestionnaireById(Convert.ToInt32(set.Tables[0].Rows[i][1]));
                        set.Tables[0].Rows[i].Delete();
                        break;
                    }

                }
                this.Repeater1.DataSource = set;
                this.Repeater1.DataBind();

            }
            if (e.CommandName == "modify")
            {

                Response.Redirect("t_paper_modify.aspx?id=" + Convert.ToInt32(e.CommandArgument), false);
                return;
            }
            if (e.CommandName == "show")
            {
                Response.Redirect("t_show.aspx?id=" + Convert.ToInt32(e.CommandArgument), false);
                return;
            }
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message;
            Session["AtPage"] = "t_paper_check.aspx";
            Response.Redirect("t_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("t_paper_edit.aspx",false);
        return;
    }
}
